package com.heakey.domain.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heakey.common.constant.CommonConstant;
import com.heakey.common.model.PageReq;
import com.heakey.common.model.PageResult;
import com.heakey.common.util.PageUtil;
import com.heakey.domain.dao.user.entity.UserRecord;
import com.heakey.domain.dao.user.mapper.UserRecordMapper;
import com.heakey.domain.model.user.bo.UserRecordBO;
import com.heakey.domain.model.user.dto.UserRecordDTO;
import com.heakey.domain.service.user.IUserRecordService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.function.Supplier;

/**
 * @Description TODO
 * @Author Bing
 * @Date 2025/6/5 9:08
 */
@Service
public class UserRecordService extends ServiceImpl<UserRecordMapper, UserRecord> implements IUserRecordService {

    @Override
    public PageResult<UserRecordDTO> queryPage(UserRecordBO param, PageReq pageReq) {
        LambdaQueryWrapper<UserRecord> wrapper = buildWrapper(() -> param);
        Page<UserRecord> page = this.page(PageUtil.toPage(pageReq), wrapper);
        List<UserRecord> pageList = page.getRecords();
        if(CollUtil.isEmpty(pageList)){
            return PageResult.empty();
        }
        // 列表转换
        PageResult<UserRecordDTO> pageResult = PageResult.success(page,(list)->{
           return BeanUtil.copyToList(list, UserRecordDTO.class);
        });
        return pageResult;
    }

    /**
     * 构建查询条件
     * @param supplier
     * @return
     */
    private LambdaQueryWrapper<UserRecord> buildWrapper(Supplier<UserRecordBO> supplier){
        UserRecordBO param = supplier.get();
        LambdaQueryWrapper<UserRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRecord::getIsDelete, CommonConstant.NOT_DELETE);
        if(StrUtil.isNotBlank(param.getNickName())){
            wrapper.like(UserRecord::getNickName, param.getNickName());
        }
        wrapper.orderByDesc(UserRecord::getCreateTime);
        return wrapper;
    }
}